using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {

    #region public params
    public bool isSecPlayer = false;
    //移动相关
    public float moveSpeed = 2f;

    //图片相关
    public GameObject picture;

    //跳跃相关
    public float fallMultiplier = 2.5f;
    public float lowJumpMultiplier = 2.0f;

    public float jumpForce = 1; //跳跃向上的力
    #endregion

    #region private params
    //移动相关
    private float horizontal;
    private Rigidbody2D rb;

    //跳跃相关
    private bool isGrounded;

    //动画以及图片相关
    private SpriteRenderer spriteRenderer;
    private Animator animator;
    #endregion

    private void Awake () {
        this.rb = this.GetComponent<Rigidbody2D> ();
        this.spriteRenderer = this.picture.GetComponent<SpriteRenderer> ();
        this.animator = this.picture.GetComponent<Animator> ();
    }

    // Start is called before the first frame update
    void Start () {
        Physics2D.queriesStartInColliders = false; //设置射线不要检测到自身
    }

    // Update is called once per frame
    void Update () {
        if (!this.isSecPlayer) {
            //检测移动输入
            if(Input.GetKey(KeyCode.A)){
                this.horizontal = -1;
            }
            if(Input.GetKey(KeyCode.D)){
                this.horizontal = 1;
            }
            if(!(Input.GetKey(KeyCode.A)||Input.GetKey(KeyCode.D))){
                this.horizontal = 0;
            }
            //检测是否在地面上
            this.isGrounded = GameManager.checkIsGround (this.transform.position);
            //检测按键
            if (Input.GetKeyDown (KeyCode.W) && this.isGrounded) {
                this.rb.AddForce (Vector2.up * jumpForce * 100);
            }
        }
        else{
            //检测移动输入
            if(Input.GetKey(KeyCode.LeftArrow)){
                this.horizontal = -1;
            }
            if(Input.GetKey(KeyCode.RightArrow)){
                this.horizontal = 1;
            }
            if(!(Input.GetKey(KeyCode.LeftArrow)||Input.GetKey(KeyCode.RightArrow))){
                this.horizontal = 0;
            }
            //检测是否在地面上
            this.isGrounded = GameManager.checkIsGround (this.transform.position);
            //检测按键
            if (Input.GetKeyDown (KeyCode.UpArrow) && this.isGrounded) {
                this.rb.AddForce (Vector2.up * jumpForce * 100);
            }
        }

        //根据移动方向翻转图片
        this.flipPicture ();

        //设置动画相关
        this.setAnim ();

        //设置跳跃相关
        this.setJump ();
    }

    private void FixedUpdate () {
        this.rb.velocity = new Vector2 (this.horizontal * moveSpeed, this.rb.velocity.y);
    }

    private void flipPicture () {
        if (this.horizontal > 0.05f) {
            this.spriteRenderer.flipX = false;
        }
        if (this.horizontal < -0.05f) {
            this.spriteRenderer.flipX = true;
        }
    }

    private void setAnim () {
        this.animator.SetFloat ("velocityX", this.horizontal);
        this.animator.SetFloat ("velocityY", this.rb.velocity.y);
        this.animator.SetBool ("isGrounded", this.isGrounded);
    }

    private void setJump () {
        if (this.rb.velocity.y < -0.05) {
            this.rb.velocity += Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
        } else if (this.rb.velocity.y > 0.05) {
            this.rb.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime;
        }
    }
}